TITLE PDP-8 Instruction Parser                        (main.asm)

; Description:
;
; Revision date:

INCLUDE Irvine32.inc
.data
instructions        WORD    000b, 001b, 010b, 011b, 100b, 101b, 110b, 111b
instructionNames    BYTE    "AND", 0, "TAD", 0, "ISZ", 0, "DCA", 0, "JMS", 0, "JMP", 0, "IOT", 0, "OPR", 0

instruction        BYTE    ?
indirectBit        BYTE    ?
pageBit            BYTE    ?
address            BYTE    ?
sampleInstruction    WORD    0000111110010111b,0000110101101011b,0000111001010110b,0000000101101111b

.code

;;Parses the address
GetAddress PROC
    ;backup the instruction in DX
    MOV DX, AX

    ;AND out the address portion of the instruction
    AND AX, 0000000001111111b
    
    ;store the address
    MOV address, AL
    
    ;restore AX
    MOV AX, DX
    
    RET
GetAddress ENDP

;;Parses the page bit
GetPageBit PROC USES EDX
    ;backup the instruction in DX
    MOV DX, AX

    AND AX, 0000000010000000b
    
    ;AND out the page bit
    .IF AX == 0000000010000000b
        MOV pageBit, 1
    .ELSE
        MOV pageBit, 0
    .ENDIF
    
    ;restore AX
    MOV AX, DX
    
    RET
GetPageBit ENDP

;;Parses the indirect bit
GetIndirectBit PROC USES EDX
    ;backup the instruction in DX
    MOV DX, AX

    ;AND out the indirect bit
    AND AX, 0000000100000000b
    
    .IF AX == 0000000100000000b
        MOV indirectBit, 1
    .ELSE
        MOV indirectBit, 0
    .ENDIF
    
    ;restore AX
    MOV AX, DX
    
    RET
GetIndirectBit ENDP

;;Parses the instruction in AX
GetInstruction PROC USES EDX
    ;backup the instruction in DX
    MOV DX, AX
    
    ;shift right to put the instruction in the bottom 3 bits
    SHR AX, 9
    
    ;Move the AL into instruction
    MOV instruction, AL
    
    ;Restore AX
    MOV AX, DX
    
    RET
GetInstruction ENDP

;;Prints the mnemonic for the instruction in instruction
PrintInstructionName PROC USES ECX EDX
    ;clear out ECX, then fill the lower 8 bits with the instruction
    MOV ECX, 0
    MOV CL, instruction

    ;multiply the instruction's numerical value by 4
    SHL ECX, 2
    
    ;move the beginning of the instruction mnemonics into EDX
    MOV EDX, OFFSET [instructionNames]
    
    ;move EDX to the name of the proper instruction
    ADD EDX, ECX

    CALL WriteString

    RET
PrintInstructionName ENDP

OutputPDP8AddressAsOctal PROC USES EAX
    ;Write the first digit of the address
    MOV EAX, 0
    MOV AL, address
    AND AL, 11000000b
    SHR AL, 6
    CALL WriteDec
    
    ;Write the second digit of the address
    MOV EAX, 0
    MOV AL, address
    AND AL, 00111000b
    SHR AL, 3
    CALL WriteDec
    
    ;Write the second digit of the address
    MOV EAX, 0
    MOV AL, address
    AND AL, 00000111b
    CALL WriteDec
    
    RET
OutputPDP8AddressAsOctal ENDP

main PROC
    MOV ECX, LENGTHOF sampleInstruction
    MOV ESI, OFFSET [sampleInstruction]
    printInstruction:
        MOV EAX, [ESI]
        
        CALL GetInstruction
        CALL GetIndirectBit
        CALL GetPageBit
        CALL GetAddress
        
        CALL PrintInstructionName
        CALL crlf

        ;print the indirect bit
        MOV EAX, 0
        MOV AL, indirectBit
        CALL WriteDec
        CALL crlf

        ;print the page bit
        MOV EAX, 0
        MOV AL, pageBit
        CALL WriteDec
        CALL crlf

        ;print the address
        CALL OutputPDP8AddressAsOctal

		ADD ESI, 2
		CALL crlf
		CALL crlf
    LOOP printInstruction

    CALL crlf
    CALL WaitMsg
    exit
main ENDP

END main